function hfile = srf_RecoSMToSphere(hfile, settings)
% SRF::RecoSMToSphere  - perform all steps to get RECOSM to sphere
%
% FORMAT:       srf = srf.RecoSMToSphere([settings])
%
% Input fields:
%
%       settings    optional settings
%        .distciter number of final distortion iterations (default: 0)
%        .force     boolean flag, don't heed filename
%        .smpsmooth curvature smoothing steps (default: [5, 20, 100])
%
% Output fields:
%
%       srf         altered object
%       densm       density map
%
% Note: this method calls srf.Smooth, srf.Inflate and srf.ToSphere

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% global settings from config
global bvqxfile_config;

% argument check
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'srf')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to ''%s''.', ...
        mfilename ...
    );
end
if nargin < 2 || ...
   ~isstruct(settings) || ...
    numel(settings) ~= 1
    settings = struct;
end
if ~isfield(settings, 'distciter') || ...
    numel(settings.distciter) ~= 1 || ...
   ~isa(settings.distciter, 'double') || ...
    isinf(settings.distciter) || ...
    isnan(settings.distciter)
    try
        settings.distciter = ...
            bvqxfile_config.settings.Morphing.ToSphere.DistCIterations;
    catch
        settings.distciter = 10000;
    end
else
    settings.distciter = floor(min(50000, max(0, settings.distciter)));
end
if ~isfield(settings, 'force') || ...
    numel(settings.force) ~= 1 || ...
   ~islogical(settings.force)
    settings.force = false;
end
if ~isfield(settings, 'smpsmooth') || ...
   ~isa(settings.smpsmooth, 'double') || ...
    numel(settings.smpsmooth) ~= 3 || ...
    any(isinf(settings.smpsmooth) | isnan(settings.smpsmooth) | ...
        settings.smpsmooth < 1 | settings.smpsmooth > 250)
    try
        settings.smpsmooth = ...
            bvqxfile_config.settings.Morphing.Curvature.SmoothingSteps;
    catch
        settings.smpsmooth = [5, 20, 100];
    end
end

% get super content
sc = bvqxfile_getscont(hfile.L);

% check filename
if (numel(sc.F) < 11 || ...
    ~strcmpi(sc.F(end-10:end), '_recosm.srf')) && ...
   ~settings.force
    error( ...
        'BVQXtools:InvalidFilename', ...
        'Filename is not *_RECO.srf. Processing not forced.' ...
    );
end
[sf{1:3}] = fileparts(sc.F);

% create curvature maps
csmp = srf_CurvatureMap(hfile);
for sc = 1:numel(settings.smpsmooth)
    smp_Smooth(csmp, hfile, settings.smpsmooth(sc), 1);
end
aft_SaveAs(csmp, [sf{1} '/' sf{2} '_CURVATURE.smp']);
aft_ClearObject(csmp);

% perform inflation
[hfile, d2] = srf_Inflate(hfile);
aft_SaveAs(hfile, [sf{1} '/' sf{2} '_INFL.srf']);
aft_SaveAs(d2, [sf{1} '/' sf{2} '_INFL_DENSITY.smp']);
aft_ClearObject(d2);

% perform to-sphere morphing
[hfile, d3] = srf_ToSphere(hfile);
aft_SaveAs(hfile, [sf{1} '/' sf{2} '_INFL_SPHERE.srf']);
aft_SaveAs(d3, [sf{1} '/' sf{2} '_INFL_SPHERE_DENSITY.smp']);
aft_ClearObject(d3);

% perform optional DC
if settings.distciter > 0
    [hfile, d4] = srf_ToSphere(hfile, settings.distciter, 0.2, 0.002);
    aft_SaveAs(hfile, [sf{1} '/' sf{2} '_INFL_SPHERE_DC.srf']);
    aft_SaveAs(d4, [sf{1} '/' sf{2} '_INFL_SPHERE_DC_DENSITY.smp']);
    aft_ClearObject(d4);
end
